package com.pi.push.app.getui.factory;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.pi.push.app.dto.PushInfo;

/**
 * @author chenmfa
 * @description 个推通知模板
 */
@SuppressWarnings("unused")
public class GtTemplateFactory {
  
  private static Logger logger = LoggerFactory.getLogger(GtTemplateFactory.class);
  //默认标题
  private static String title = "App提醒";
  //默认文字内容
  //private static String text = "";
  //默认图标
  private static String logo = "icon.png";
  //默认图标的url
  private static String logoUrl = "";
  //默认点击通知后显示的网页
  private static String openUrl = "";
  //默认响铃提醒
  private static String isRing = "true";
  //默认震动提醒
  private static String isVibrate = "true";
  //默认通知可以清除
  private static String isClearable = "true";
  
  private static Integer transmissionType = 2;
//  个推旧版本的badge
  private static Integer badge = 1;
//  个推新版本的
  private static String autobadge = "+1";
  
  private static Integer contentAvailable = 1;
  
  private static String sound = "default";
  
  private GtTemplateFactory(){
  }
  
  /**
   * @Description 个推新版1.0.1.9已废弃TransmissionTemplate的 APNPayload.setBadge和方法
   * @param pushInfo
   * @param msgTag
   * @return
   */
  public static TransmissionTemplate getTransmissionTemplate(PushInfo pushInfo,String msgTag){
    String phoneType = pushInfo.getDeviceType().toString();
    TransmissionTemplate template = new  TransmissionTemplate();
    template.setAppId(pushInfo.getPushConfig().getAppId());
    template.setAppkey(pushInfo.getPushConfig().getAppKey());
    template.setTransmissionType(transmissionType);
    //消息格式: 消息类型|发送者标志号码|发送者别名|消息|接受者账号|接收者别名|发送时间
    StringBuilder sb =new StringBuilder();
    sb.append(pushInfo.getPushTemplate()+"|");
    sb.append(pushInfo.getPushSourceId()+"|");
    sb.append(pushInfo.getPushSourceName()+"|");
    sb.append(pushInfo.getPushMsg()+"|");
    sb.append(pushInfo.getTargetReceiverId()+"|");
    sb.append(pushInfo.getTargetReceiverName()+"|");
    sb.append(pushInfo.getMsgTime()+"|");
    
    logger.info("{}消息主体:{}",msgTag,sb.toString());
    
    if("ios".equalsIgnoreCase(phoneType)){
      template.setTransmissionContent(sb.toString());
      APNPayload payload = new APNPayload();
      //payload.setBadge(badge);
      //payload.setAutoBadge(autobadge);//这里不能使用新的个推api的方法,好像新的会导致不兼容等问题
      payload.setContentAvailable(contentAvailable);
      payload.setSound(sound);
      payload.addCustomMsg("msg", sb.toString());
      //payload.addCustomMsg(key, value);
      //设置触发特定的action或按钮显示，暂时设为无
      //payload.setCategory("");
      //APNPayload.SimpleMsg  设置通知消息体
//        payload.setBadge(badge);       
      payload.setAutoBadge(autobadge);//新版本采用新的api接口
      payload.setAlertMsg(new APNPayload.SimpleAlertMsg("您有一条新的消息"));
      //payload.setAlertMsg(getDictionaryAlertMsg());
      template.setAPNInfo(payload);
    }else{
      template.setTransmissionContent(sb.toString()); 
    }
    return template;
  }
}
